% LaTeX Beamer inner theme "chamfered"
%
% Copyright Christian Gogolin 2011 (beamer@cgogolin.de)
%
% http://www.cgogolin.de
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%   features: - Boxes with arbitrary rounding radius, content-to-box-
%               boder and head-to-body distance, customizable via the
%               [corners=] and [padding=] options. Both are set to a
%               default value of 2pt.
%             - A drop shadow that can be activated with the [shadow]
%               option that is compatible with all box shapes(!).
%             - A real drop shadow that can be activated with the [realshadow]
%               option that is compatible with all box shapes(!) and is really opaque.
%             - A surrounding outline can be activated by giving the
%               [outline] options.
%             - Draws the whole box "at once" (and not in two steps, as
%               the normal beamer colorbox does) using the TikZ-Package
%               and thus has a very easy to read code structure.
%  
%   problems: - Uses your TikZ-Package (Not really a problem, I think.)
%             - Overwrites the "\beamerboxesrounded" and the
%               "\endbeamerboxesrounded" commands. (Not really a problem
%               as well, I think.)
% 
%   to do:    - Jet no custom parts page and only a very minimal
%               title page.
%             - Perhaps add custom "items" and "sections/subsections in
%               toc" templates.

\RequirePackage{keyval}
\RequirePackage{tikz}
\RequirePackage{mathtools}

\newif\ifbeamer@chamfered@shadow
\DeclareOptionBeamer{shadow}[true]{\csname beamer@chamfered@shadow#1\endcsname}
\ExecuteOptionsBeamer{shadow=false}

\newif\ifbeamer@chamfered@realshadow
\DeclareOptionBeamer{realshadow}[true]{\csname beamer@chamfered@realshadow#1\endcsname}
\ExecuteOptionsBeamer{realshadow=fal1se}

\newif\ifbeamer@chamfered@outline
\DeclareOptionBeamer{outline}[true]{\csname beamer@chamfered@outline#1\endcsname}
\ExecuteOptionsBeamer{outline=false}

\newdimen\bmb@corners
\DeclareOptionBeamer{corners}{\bmb@corners=#1}
\ExecuteOptionsBeamer{corners=2pt}

\newdimen\bmb@padding
\DeclareOptionBeamer{padding}{\bmb@padding=#1}
\ExecuteOptionsBeamer{padding=2pt}

\ProcessOptionsBeamer

\mode<presentation>

\newdimen\bmb@headheight
\newdimen\bmb@headdepth
\newdimen\bmb@bodyheight
\newdimen\bmb@bodydepth
%\newdimen\bmb@corners
%\newdimen\bmb@padding
\newdimen\bmb@headbodyseperation

\newlength{\tmpa}

\newbox\bmb@headbox
\newbox\bmb@bodybox

\define@key{beamerboxes}{realshadow}[false]{\csname bmb@realshadow#1\endcsname}
\define@key{beamerboxes}{outline}[false]{\csname bmb@outline#1\endcsname}

\renewcommand\beamerboxesrounded[2][]{%
  \global\let\beamer@firstlineitemizeunskip=\relax%
  \vbox\bgroup%
  \setkeys{beamerboxes}{upper=block title,lower=block body,width=\textwidth,shadow=false,realshadow=false}%
  \setkeys{beamerboxes}{#1}%
  {%
    \usebeamercolor{\bmb@lower}%
    \globalcolorstrue%
    \colorlet{lower.bg}{bg}%
  }%
  {%
    \usebeamercolor{\bmb@upper}%
    \globalcolorstrue%
    \colorlet{upper.bg}{bg}%
  }%
  % lengths are set via beameroptions
%  \bmb@corners=2pt
%  \bmb@padding=2pt
  % 
  \setbox\bmb@headbox=\hbox{%
    \begin{minipage}[b]{\bmb@width-\bmb@corners-\bmb@corners-\bmb@padding-\bmb@padding}%
      \usebeamercolor[fg]{\bmb@upper}%
      #2%
    \end{minipage}}%
  \ifdim\wd\bmb@headbox=0pt%
    \setbox\bmb@headbox=\hbox{}%
    \ht\bmb@headbox=0pt%
    \dp\bmb@headbox=0pt%
    \bmb@headheight=0pt%
    \bmb@headdepth=1.4ex% 
    \bmb@headbodyseperation=0pt%
    {\usebeamercolor{\bmb@lower}% make head and body bg color equal if head is empty
     \globalcolorstrue%
     \colorlet{upper.bg}{bg}}%
  \else
    \bmb@headheight=\ht\bmb@headbox
    \bmb@headdepth=1.5ex%
    \bmb@headbodyseperation=\bmb@padding
  \fi%

  \setbox\bmb@bodybox=\hbox\bgroup\begin{minipage}[b]{\bmb@width-\bmb@corners-\bmb@corners-\bmb@padding-\bmb@padding}%
    \usebeamercolor[fg]{\bmb@lower}%
    \colorlet{beamerstructure}{upper.bg}%
    \colorlet{structure}{upper.bg}%
  }


\def\endbeamerboxesrounded{%
  \end{minipage}\egroup%
  \ifdim\wd\bmb@bodybox=0pt%
    \setbox\bmb@bodybox=\hbox{}%
    \ht\bmb@bodybox=0pt%
    \dp\bmb@bodybox=0pt%
    \bmb@bodyheight=1pt%
    \bmb@bodydepth=\bmb@padding%
  \else
    \bmb@bodyheight=\ht\bmb@bodybox\advance\bmb@headheight by\bmb@corners
    \bmb@bodydepth=1.5ex%
  \fi%

  \begin{tikzpicture}
    \ifbeamer@chamfered@shadow%
      \foreach \x in {75,50,15}
      {
        \setlength{\tmpa}{1.25pt * 1/75 * \real{\x}}
        \fill[xshift=0.4pt,yshift=-0.5pt,fill=lower.bg!\x!black,rounded corners=\bmb@corners] (-\tmpa,-\bmb@headdepth-\bmb@headbodyseperation) -- (-\tmpa,\bmb@headheight+\bmb@padding+0.5pt+\tmpa) -- (\bmb@width+\tmpa,\bmb@headheight+\bmb@padding+0.5pt+\tmpa) -- (\bmb@width+\tmpa,-\bmb@headdepth-\bmb@headbodyseperation) -- (\bmb@width+\tmpa,-\bmb@headdepth-\bmb@headbodyseperation-\bmb@bodyheight-\bmb@bodydepth-\bmb@padding-\bmb@corners-\tmpa) -- (-\tmpa,-\bmb@headdepth-\bmb@headbodyseperation-\bmb@bodyheight-\bmb@bodydepth-\bmb@padding-\bmb@corners-\tmpa) -- (-\tmpa,-\bmb@headdepth-\bmb@headbodyseperation);
      }
      \fi

    \ifbeamer@chamfered@realshadow%
      \foreach \x in {75,50,15}
      {
        \setlength{\tmpa}{1.25pt * 1/75 * \real{\x}}
        \fill[xshift=0.4pt,yshift=-0.5pt,fill=black,opacity=0.3,rounded corners=\bmb@corners] (-\tmpa,-\bmb@headdepth-\bmb@headbodyseperation) -- (-\tmpa,\bmb@headheight+\bmb@padding+0.5pt+\tmpa) -- (\bmb@width+\tmpa,\bmb@headheight+\bmb@padding+0.5pt+\tmpa) -- (\bmb@width+\tmpa,-\bmb@headdepth-\bmb@headbodyseperation) -- (\bmb@width+\tmpa,-\bmb@headdepth-\bmb@headbodyseperation-\bmb@bodyheight-\bmb@bodydepth-\bmb@padding-\bmb@corners-\tmpa) -- (-\tmpa,-\bmb@headdepth-\bmb@headbodyseperation-\bmb@bodyheight-\bmb@bodydepth-\bmb@padding-\bmb@corners-\tmpa) -- (-\tmpa,-\bmb@headdepth-\bmb@headbodyseperation);
      }
      \fi

    \ifbeamer@chamfered@outline% The 1pt offset added to \bmb@headdepth is a hack to fix the rounding errors of some stupid pdf viewers!
      \filldraw[fill=upper.bg,draw=structure.fg,thin,rounded corners=\bmb@corners] (0pt,-\bmb@headdepth-1pt) -- (0pt,\bmb@headheight+\bmb@padding+0.5pt) -- (\bmb@width,\bmb@headheight+\bmb@padding+0.5pt) -- (\bmb@width,-\bmb@headdepth-1pt);
      \filldraw[fill=lower.bg,draw=structure.fg,thin,rounded corners=\bmb@corners] (\bmb@width,-\bmb@headdepth+1pt) -- (\bmb@width,-\bmb@headdepth-\bmb@headbodyseperation-\bmb@bodyheight-\bmb@bodydepth-\bmb@padding-\bmb@corners) -- (0pt,-\bmb@headdepth-\bmb@headbodyseperation-\bmb@bodyheight-\bmb@bodydepth-\bmb@padding-\bmb@corners) -- (0pt,-\bmb@headdepth+1pt);
    \else
      \fill[fill=upper.bg,rounded corners=\bmb@corners] (0pt,-\bmb@headdepth-1pt) -- (0pt,\bmb@headheight+\bmb@padding+0.5pt) -- (\bmb@width,\bmb@headheight+\bmb@padding+0.5pt) -- (\bmb@width,-\bmb@headdepth-1pt);
      \fill[fill=lower.bg,rounded corners=\bmb@corners] (\bmb@width,-\bmb@headdepth+1pt) -- (\bmb@width,-\bmb@headdepth-\bmb@headbodyseperation-\bmb@bodyheight-\bmb@bodydepth-\bmb@padding-\bmb@corners) -- (0pt,-\bmb@headdepth-\bmb@headbodyseperation-\bmb@bodyheight-\bmb@bodydepth-\bmb@padding-\bmb@corners) -- (0pt,-\bmb@headdepth+1pt);
    \fi
    \pgfputat{\pgfpoint{\bmb@padding+\bmb@corners}{0pt}}{\pgftext[left,base]{\copy\bmb@headbox}}
    \pgfputat{\pgfpoint{\bmb@padding+\bmb@corners}{-\bmb@headdepth-\bmb@headbodyseperation-\bmb@bodyheight}}{\pgftext[left,base]{\copy\bmb@bodybox}}
  \end{tikzpicture}
  \egroup% of \vbox\bgroup
}


%% Gionata Massi

\defbeamertemplate*{title page}{chamfered}[1][]
{
  \vbox{}
  \vfill
  \begin{centering}
    \begin{centering}
     \begin{centering}
      \usebeamerfont{institute}\usebeamercolor[fg]{institute}\insertinstitute
    \end{centering}
    \vskip1em\par
      \usebeamerfont{title}\usebeamercolor[fg]{title}\inserttitle\par%
      \ifx\insertsubtitle\@empty%
      \else%
      \vskip0.25em%
      {\usebeamerfont{subtitle}\usebeamercolor[fg]{subtitle}\insertsubtitle\par}%
      \fi%     
    \end{centering}
    \vskip1.5em\par
    \begin{centering}
      \usebeamerfont{author}\usebeamercolor[fg]{author}\insertauthor
    \end{centering}
    \vskip1em\par
   \begin{centering}
      \usebeamerfont{date}\usebeamercolor[fg]{date}\insertdate
    \end{centering}\vskip0.5em
    {\usebeamercolor[fg]{titlegraphic}\inserttitlegraphic\par}
  \end{centering}
  \vfill
}




\setbeamertemplate{sections/subsections in toc}[square]
\setbeamertemplate{items}[square]
\setbeamertemplate{blocks}[rounded]

\setbeamertemplate{title page}[chamfered]
\setbeamertemplate{part page}[default]


\mode<all>

%%% Local Variables: 
%%% TeX-master: "~/texmf/tex/latex/beamer/test/test.tex"
%%% End: 
